﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.Common;
using spbtzb;

public partial class Back_BgArticleAdd : System.Web.UI.Page
{
    #region Field

    protected String StrConn = ConString.ConnString;
    private Boolean session_Logined
    {
        set
        {
            Session["Logined"] = value;
        }
        get
        {
            return Session["Logined"] == null ? false : Convert.ToBoolean(Session["Logined"]);
        }
    }
    private String view_FatherType
    {
        get
        {
            return ViewState["view_FatherType"] == null ? "" : Convert.ToString(ViewState["view_FatherType"]);
        }
        set
        {
            ViewState["view_FatherType"] = value;

        }
    }

    private String view_ChildType
    {
        get
        {
            return ViewState["view_ChildType"] == null ? "" : Convert.ToString(ViewState["view_ChildType"]);
        }
        set
        {
            ViewState["view_ChildType"] = value;

        }
    }
    #endregion

    #region 

    
    public  DataTable ParentType 
    {
        get 
        {

 //         string sqlCmd = "SELECT * FROM ContentType WHERE ParentType = 'NO' OR ParentType = 'NOBB' OR ParentType = 'STBB'";
            string sqlCmd = "SELECT distinct C.* FROM ContentType C ,ContentType CT WHERE (( C.ParentType = 'NO' OR C.ParentType = 'STBB') And (C.TypeCode= CT.ParentType)) OR C.ParentType='NOBB'";
            System.Data.DataTable dtTypes = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
            
                if (dtTypes.Rows.Count == 0)//dtTypes == null || 
                {
                    // 没有相应类型的信息
                    RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
                    return new DataTable();
                }
            return dtTypes;
        }

    }


    public string SpValueID 
    {
        get 
        {
           return  SpValue.ClientID;
        }
    }
    #region 刘涛修改
    public int MaxID //获得最大ID
    {
        get
        {
            string sqlCmd = "SELECT * FROM ContentType order by ID desc";
            System.Data.DataTable dtTypes = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
            if (dtTypes.Rows.Count == 0)//dtTypes == null || 
            {
                // 没有相应类型的信息
                RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
                return 0;
            }
            return int.Parse(dtTypes.Rows[0][0].ToString());
        }
    }

    public string ContentClientID //获得ftb的客户端ID
    {
        get
        { 
            return ftb_Content.ClientID;
        }
    }

    public string FieldClientID //获得Field的客户端ID
    {
        get
        {
            return chkvalue.ClientID;
        }
    }
    #endregion

    public DataTable ChildType 
    {
        get 
        {
            string sqlCmd = String.Format("SELECT * FROM ContentType where IsSingle <> 1");
            
            System.Data.DataTable dtChildTypes = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
            if (dtChildTypes.Rows.Count == 0) 
            {
                RegisterStartupScript("没有相应数据", "<script>alert('没有相关的数据！');</script>");
                return new DataTable();
            }
            
            return dtChildTypes;
        }
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (session_Logined == false)
            {
                Response.Redirect("Login.aspx?ErrorMessage=" + "您未登录，请在此登录");
            }
            if (Request["fathertype"] == null)
            {
                // 跳转到错误处理页面
                Response.Redirect("~/Common/ErrorPage.aspx?ErrorMessage=" + "跳转参数为空，请联系管理员!");
            }
            else
            {
                view_FatherType = Request["fathertype"].ToString();
                if (Request["childtype"] != null) view_ChildType = Request["childtype"].ToString();
                Ddl_Types_BindData();
 
            }
                    
         
           //StrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='E:\\spbtzbDB\\spbtzb.mdb';";        
            //ddl_Types_SelectedIndexChanged(new Object(), new EventArgs());//ddl_Types数据绑定之后,将其子类型绑定ddl_ChildType
        }
    }

    protected void Submit_Btn_Click(object sender, EventArgs e)
    {
        
        String strContent = spbtzb.AuthenCtr.Encode(this.ftb_Content.Text.TrimEnd());
        String strTypeCode = "";
        if (this.ddl_ChildTypes.Visible == true)
        {
            strTypeCode = this.ddl_ChildTypes.SelectedValue;
        }
        else
        {
            strTypeCode = this.ddl_Types.SelectedValue;
        }
        
        string sqlCmd = String.Format("INSERT INTO Contents (TypeCode, Title, Content, CreateTime) "
                       + "values ('{0}', '{1}', '{2}', '{3}' )", "1",
                       this.tb_Title.Text.Trim(), strContent, DateTime.Now.ToString());
        int iRet = AccessHelper.ExecuteNonQuery(StrConn, System.Data.CommandType.Text, sqlCmd);


        DataTable dataTable=  AccessHelper.ExecuteDataTable(StrConn, CommandType.Text, "select max(ID) AS S from Contents");
        int ID =int.Parse( dataTable.Rows[0]["S"].ToString());
        List<string> listType;
        if (chkvalue.Text == "")
        {
            listType = new List<string>();
        }
        else
        {
            listType = chkvalue.Text.Split(',').ToList();
        }
        if (SpValue.Text != "")
        {
            listType.Add(SpValue.Text);
        }
        
        
        foreach (var c in listType) 
        {

            string sqlGetTypeID =  String.Format("Select ID From ContentType Where TypeCode='{0}'", c);
            DataTable datatable1= AccessHelper.ExecuteDataTable(StrConn, CommandType.Text, sqlGetTypeID);
            int TypeID = int.Parse(datatable1.Rows[0]["ID"].ToString());
            string sqlCmd1 = String.Format("Insert into ContentAndType (ContentID,TypeID,ContentType) values('{0}','{1}','{2}')",ID,TypeID,c);
            AccessHelper.ExecuteNonQuery(StrConn, CommandType.Text, sqlCmd1);
        } 
        if (iRet > 0)
        {
            Response.Write("<script>alert('文章添加成功！');</script>");
        }
        else
        {
            // 跳转到错误处理页面
            Response.Redirect("~/Common/ErrorPage.aspx?ErrorMessage=" + "添加文章操作失败，请联系管理员!");
        }
    }
    protected void Cancel_Btn_Click(object sender, EventArgs e)
    {
        Response.Redirect("BgArticleList.aspx");
        //RegisterClientScriptBlock("tip", "<script>window.close();</script>");
    }

    private void Ddl_Types_BindData()
    {
        // generate datatable                       
        string sqlCmd = "SELECT * FROM ContentType WHERE ParentType = 'NO' OR ParentType = 'NOBB' OR ParentType = 'STBB'";       
        System.Data.DataTable dtTypes = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
        if (dtTypes.Rows.Count == 0)//dtTypes == null || 
        {
            // 没有相应类型的信息
            RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
            return;
        }
       
        this.ddl_Types.DataSource = dtTypes;
        this.ddl_Types.DataBind();
        this.ddl_Types.SelectedValue = view_FatherType; // edited by shiney 11.3
        ddl_Types_SelectedIndexChanged(new Object(), new EventArgs());

        // edited by shiney 11.3
        if (!String.IsNullOrEmpty(view_ChildType))
        {
            ddl_ChildTypes.SelectedValue = view_ChildType;
        }

    }

    protected void btn_Reset_Click(object sender, EventArgs e)
    {
        this.tb_Title.Text = "";
        this.ftb_Content.Text = "";
    }
    protected void ddl_Types_SelectedIndexChanged(object sender, EventArgs e)
    {
        // generate datatable                       
        string sqlCmd = String.Format("SELECT * FROM ContentType WHERE ParentType = '{0}'", this.ddl_Types.SelectedValue);
        System.Data.DataTable dtChildTypes = AccessHelper.ExecuteDataTable(StrConn, System.Data.CommandType.Text, sqlCmd);
        if (dtChildTypes == null || dtChildTypes.Rows.Count == 0)
        {
            // 没有相应类型的信息
            //RegisterClientScriptBlock("tip", "<script>alert('没有相应的类型数据！');</script>");
            this.ddl_ChildTypes.Visible = false;
            return;
        }
        this.ddl_ChildTypes.Visible = true;
        this.ddl_ChildTypes.DataSource = dtChildTypes;
        this.ddl_ChildTypes.DataBind();
    }
    
}
